package com.lanmaoly.cunzheng.thread;

public class DelLock {
    static Object lockA = new Object();
    static Object lockB = new Object();

    public static void main(String[] args) {
        //线程要执行完，必须获取lockA和lockB
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lockA) {
                    System.out.println("第一个线程调用A");
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (lockB) {
                        System.out.println("第一个线程调用B");
                    }
                }
            }
        }).start();
        //线程要执行完，必须获取lockB和lockA
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lockB) {
                    System.out.println("第二个线程调用B");
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (lockA) {
                        System.out.println("第二个线程调用A");
                    }
                }
            }
        }).start();
    }
}